Refactor file management script to use asyncio and correct event#225
Refactor file management script to use asyncio and correct event#225pierlag wants to merge 1 commit intogithub:mainfrom
Conversation
Fix event object properties and add asyncio
There was a problem hiding this comment.
Pull request overview
This PR attempts to refactor the file management script to use Python's asyncio for asynchronous operations and updates event property access patterns from dictionary-style to object attribute access.
Changes:
- Wraps the main code in an async function and uses
asyncio.run() - Updates
CopilotClientmethod calls to be async (withawait) - Changes event property access from
event["data"]["content"]toevent.data.content - Updates model from "gpt-5" to "gpt-4.1"
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if event["type"] == "assistant.message": | ||
| print(f"\nCopilot: {event.data.content}") | ||
| elif event["type"] == "tool.execution_start": | ||
| print(f" → Running: {event.data.tool_name}") | ||
| elif event["type"] == "tool.execution_complete": |
There was a problem hiding this comment.
The event type should be accessed as event.type (attribute access) rather than event["type"] (dictionary access). The SessionEvent class is a dataclass with a type attribute (see python/copilot/generated/session_events.py:808). This is inconsistent with the corrected property access on lines 18, 20, and 22 where event.data.content, event.data.tool_name, and event.data.tool_call_id are correctly used.
| if event["type"] == "assistant.message": | |
| print(f"\nCopilot: {event.data.content}") | |
| elif event["type"] == "tool.execution_start": | |
| print(f" → Running: {event.data.tool_name}") | |
| elif event["type"] == "tool.execution_complete": | |
| if event.type == "assistant.message": | |
| print(f"\nCopilot: {event.data.content}") | |
| elif event.type == "tool.execution_start": | |
| print(f" → Running: {event.data.tool_name}") | |
| elif event.type == "tool.execution_complete": |
| session.send({"prompt":f""" | ||
| Analyze the files in "{target_folder}" and organize them into subfolders. | ||
|
|
||
| 1. First, list all files and their metadata | ||
| 2. Preview grouping by file extension | ||
| 3. Create appropriate subfolders (e.g., "images", "documents", "videos") | ||
| 4. Move each file to its appropriate subfolder | ||
| 1. First, list all files and their metadata | ||
| 2. Preview grouping by file extension | ||
| 3. Create appropriate subfolders (e.g., "images", "documents", "videos") | ||
| 4. Move each file to its appropriate subfolder | ||
|
|
||
| Please confirm before moving any files. | ||
| """) | ||
| Please confirm before moving any files. | ||
| """}) |
There was a problem hiding this comment.
The session.send() method is async and must be awaited. Without await, this will return a coroutine object that is never executed, and the message will not be sent to the Copilot session.
| """}) | ||
|
|
||
| session.wait_for_idle() | ||
| session.wait() |
There was a problem hiding this comment.
The method session.wait() does not exist in the CopilotSession API. Based on the session.py source code (lines 120-179), you should use send_and_wait() instead of separate send() and wait() calls, or use the event-based approach with a session.idle event handler. The old synchronous API had wait_for_idle(), but in the async API this functionality is provided by send_and_wait().
|
|
||
| session.destroy() | ||
| client.stop() | ||
| session.destroy() |
There was a problem hiding this comment.
The session.destroy() method is async and must be awaited. Without await, the session cleanup will not complete properly, potentially leaving resources uncleaned.
| session.destroy() | |
| await session.destroy() |
| session.destroy() | ||
| client.stop() | ||
| session.destroy() | ||
| client.stop() |
There was a problem hiding this comment.
The client.stop() method is async and must be awaited. Without await, the client shutdown will not complete properly, potentially leaving the CLI process running or connections open.
| client.stop() | |
| await client.stop() |
Fix event object properties and add asyncio